﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Printing;

namespace DYSales
{
    public class PrintManager
    {
        static PrintManager pt;
        internal static PrintManager GetPrintManager()
        {
            if (pt == null)
            {
                pt = new PrintManager();
            }
            return pt;
        }

        internal static List<TextBlock> tbs;
        internal static int tbcount = 0;
        internal static double PrintHight = 1100;

        internal static TextBlock CreatePrintTextBlock(double s_页宽)
        {
            var tb = new TextBlock();
            tb.Style = null;
            tb.Width = s_页宽;
            tb.TextWrapping = TextWrapping.Wrap;
            return tb;
        }

        PrintDocument document;
        PrinterFallbackSettings settings;
        internal void DoPrint()
        {
            if (document == null)
            {
                document = new PrintDocument();
                document.PrintPage += new EventHandler<PrintPageEventArgs>(document_PrintPage);
                settings = new PrinterFallbackSettings();
                settings.ForceVector = true;
                settings.OpacityThreshold = .5;
            }
            try
            {
                document.Print("东雅软件小票", settings, true);
            }
            catch (Exception ex)
            {
                var box = other.GetMessageBox();
                box.Title = "打印错误提示";
                box.tbk_text.Text = ex.Message + "|电脑没有设置默认打印机或者鼠标右键安装到本地计算机提升权限！";
                box.Show();
            }
        }

        void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.PageVisual = tbs[tbcount];
            tbcount++;
            var d = e.PrintableArea.Width;
            if (tbcount < tbs.Count)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
            }
        }

    }
}
